package com.tech.tool.controller;

import com.tech.tool.Form.ApiReviewForm;
import com.tech.tool.dataobject.ApiReview;
import com.tech.tool.dto.DeleteStatusDTO;
import com.tech.tool.dto.apiReview.ApiReviewListDTO;
import com.tech.tool.dto.apiReview.ApiReviewStatusDTO;
import com.tech.tool.enums.ResultEnum;
import com.tech.tool.exception.ToolException;
import com.tech.tool.service.ApiReviewService;
import com.tech.tool.utils.ListVOUtil;
import com.tech.tool.utils.ResultVOUtil;
import com.tech.tool.utils.SaveVOUtil;
import com.tech.tool.vo.SaveVO;
import com.tech.tool.vo.apiReview.ApiReviewInfoVO;
import com.tech.tool.vo.apiReview.ApiReviewListVO;
import com.tech.tool.vo.ListVO;
import com.tech.tool.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/apiReview")
public class ApiReviewController {

    @Autowired
    private ApiReviewService apiReviewService;

    @PostMapping(path = "/list",
            consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    @CrossOrigin(origins = "*", maxAge = 3600)
    // TODO 入参形式
    public ResultVO list(@RequestBody ApiReviewListDTO apiReviewListDTO) {
        PageRequest pageRequest = PageRequest.of(apiReviewListDTO.getPageIndex()-1, apiReviewListDTO.getPageLimit());
        Page<ApiReview> apiReviewPage = apiReviewService.findByProjectId(
                apiReviewListDTO.getProjectId(), pageRequest);
        List<ApiReviewListVO> apiReviewListVOList = new ArrayList<>();

        for (ApiReview apiReview: apiReviewPage.getContent()) {
            ApiReviewListVO apiReviewListVO = new ApiReviewListVO();
            apiReviewListVO.setUserName(apiReview.getUserInfo().getUserName());
            BeanUtils.copyProperties(apiReview, apiReviewListVO);
            apiReviewListVOList.add(apiReviewListVO);
        }

        ListVO listVO = new ListVOUtil().setPageInfo(apiReviewPage, apiReviewListVOList);
        listVO.setList(apiReviewListVOList);

        return ResultVOUtil.success(listVO);

    }

    @PostMapping(path = "/save",
            consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    @CrossOrigin(origins = "*", maxAge = 3600)
    public ResultVO save(@RequestBody @Valid ApiReviewForm apiReviewForm, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            log.error("【创建订评审接口】参数不正确, apiReviewForm={}", apiReviewForm);
            throw new ToolException(ResultEnum.PARAM_ERROR.getCode(),
                    bindingResult.getFieldError().getDefaultMessage());
        }
        ApiReview apiReview = new ApiReview();
        BeanUtils.copyProperties(apiReviewForm, apiReview);
        ApiReview result = apiReviewService.save(apiReview);
        SaveVO saveVO = SaveVOUtil.setId(result.getId());
        return ResultVOUtil.success(saveVO);
    }

    @PostMapping(path = "/updateStatus",
            consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    @CrossOrigin(origins = "*", maxAge = 3600)
    public ResultVO updateStatus(@RequestBody ApiReviewStatusDTO apiReviewStatusDTO) {
        ApiReview result = apiReviewService.updateStatus(apiReviewStatusDTO.getId(), apiReviewStatusDTO.getApiStatus());
        SaveVO saveVO = SaveVOUtil.setId(result.getId());
        return ResultVOUtil.success(saveVO);
    }

    @PostMapping(path = "/delete",
            consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    @CrossOrigin(origins = "*", maxAge = 3600)
    public ResultVO updateDelete(@RequestBody DeleteStatusDTO deleteStatusDTO) {
        ApiReview result = apiReviewService.delete(deleteStatusDTO.getId(), deleteStatusDTO.getDeleteStatus());
        SaveVO saveVO = SaveVOUtil.setId(result.getId());
        return ResultVOUtil.success(saveVO);
    }

    @PostMapping(path = "/detail",
            consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    @CrossOrigin(origins = "*", maxAge = 3600)
    public ResultVO detail(@RequestBody DeleteStatusDTO detailDTO) {
        ApiReview apiReview = apiReviewService.findOne(detailDTO.getId());
        ApiReviewInfoVO apiReviewInfoVO = new ApiReviewInfoVO();
        BeanUtils.copyProperties(apiReview, apiReviewInfoVO);
        return ResultVOUtil.success(apiReviewInfoVO);

    }

    @GetMapping(path = "/detailget",
            consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    @CrossOrigin(origins = "*", maxAge = 3600)
    public ResultVO detail(@RequestParam("id") Integer id) {
        ApiReview apiReview = apiReviewService.findOne(id);
        ApiReviewInfoVO apiReviewInfoVO = new ApiReviewInfoVO();
        BeanUtils.copyProperties(apiReview, apiReviewInfoVO);
        return ResultVOUtil.success(apiReviewInfoVO);

    }

}
